package kiwi.assist.dev.guieditor.command {
	import kiwi.assist.dev.guieditor.model.PropertyConfig;
	import kiwi.assist.dev.guieditor.model.UiDetailVO;
	import kiwi.assist.dev.guieditor.event.GUIEditorEvent;
	import kiwi.assist.dev.guieditor.event.GUIOperateEvent;
	import kiwi.assist.dev.guieditor.model.ContainerConfig;
	import kiwi.assist.dev.guieditor.model.GUIEditorConfig;
	import kiwi.assist.dev.guieditor.model.UiConfig;
	import kiwi.assist.dev.util.SkinComponentAdapter;

	import org.robotlegs.mvcs.Command;

	import flash.display.DisplayObject;

	/**
	 * @author zhangming.luo
	 */
	public class CloneUiCommand extends Command {
		[Inject]
		public var event : GUIOperateEvent;
		[Inject]
		public var config : GUIEditorConfig;
		[Inject]
		public var detail : UiDetailVO;
		private var ui : DisplayObject;

		override public function execute() : void {
			super.execute();
			var uiConfig : UiConfig;
			ui = event.targetUi;
			if (ui is SkinComponentAdapter) {
				// 构造一个北京
				var component : SkinComponentAdapter = new SkinComponentAdapter(SkinComponentAdapter(ui).skin);
				dispatch(new GUIOperateEvent(GUIOperateEvent.BACKGROUND_ADD, component));
				component.x = event.param.x;
				component.y = event.param.y;
				component.width = ui.width;
				component.height = ui.height;
				return;
			} else {
				uiConfig = config.getUiConfigByDos(ui);
				if (uiConfig is ContainerConfig) {
					return;
				}
			}
			dispatch(new GUIEditorEvent(GUIEditorEvent.UI_CREATE, uiConfig));
			var cloneUi : DisplayObject = detail.uiList[detail.uiList.length - 1];
			for each (var property:PropertyConfig in uiConfig.propertyList) {
				if (property.key == "name") {
					continue;
				}
				cloneUi[property.key] = ui[property.key];
			}
			cloneUi.x = event.param.x;
			cloneUi.y = event.param.y;
		}
	}
}
